//
//  KXAnimation.m
//  KaiXinClient
//
//  Created by Shi Jin Lu on 11-12-19.
//  Copyright (c) 2011年 kaixin001. All rights reserved.
//

#import "KXImageAnimation.h"

#define kTransitionDuration	0.75

@implementation KXImageAnimation

+ (void)imageShowScaleAnimation:(UIView *)animationView
{
    CGFloat width = animationView.frame.size.width;
    CGFloat height = animationView.frame.size.height;
    
    animationView.transform = CGAffineTransformMakeScale(1/width, 1/height);
    [UIImageView animateWithDuration:0.3 animations:^{
        animationView.transform = CGAffineTransformMakeScale(1.1, 1.1);
        
    }completion:^(BOOL finished){
        [UIImageView animateWithDuration:0.1 animations:^{
            animationView.transform = CGAffineTransformMakeScale(0.9, 0.9);            
        }
        completion:^(BOOL finished){
            [UIImageView animateWithDuration:0.1 animations:^{
                animationView.transform = CGAffineTransformIdentity;                            
            }];
        }];
    }];
    
}

+ (void)imageShowFlipAnimation:(UIView *)animationView withType:(KXIMAGE_ANIMATION_TYPE)imageAnimationType
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.7];
    [UIView setAnimationTransition:imageAnimationType == KXIMAGE_ANIMATION_FLIP_LEFT ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight forView:animationView cache:YES];
    [UIView commitAnimations];
}

+ (void)imageShowAnimation:(UIView *)animationView withOldImg:(UIImage*) oldImg withType:(KXIMAGE_ANIMATION_TYPE)imageAnimationType
{
    if (animationView)
    {
        if (imageAnimationType == KXIMAGE_ANIMATION_RANDOM) {
            imageAnimationType = rand() % (KXIMAGE_ANIMATION_RANDOM - 1);
            imageAnimationType++;
        }
        
        switch (imageAnimationType) 
        {
            case KXIMAGE_ANIMATION_SCALE:
            {
                [self imageShowScaleAnimation:animationView];
                break;  
            }
            case KXIMAGE_ANIMATION_FLIP_LEFT:
            case KXIMAGE_ANIMATION_FLIP_RIGHT:
            {
                [self imageShowFlipAnimation:animationView withType:imageAnimationType];
                break;  
            }
            case KXIMAGE_ANIMATION_FADE_IN:
            {
                [self imageShowFadeAnimation:animationView withOldImg:oldImg];
                break;
            }
            default:
                break;
        }
    }
}

+ (void)imageShowFadeAnimation:(UIView *)animationView withOldImg:(UIImage*) oldImg
{
    if (oldImg) {
        UIImageView* coverView = [[UIImageView alloc] initWithImage:oldImg];
        coverView.frame = CGRectMake(0, 0, animationView.frame.size.width, animationView.frame.size.height);
        coverView.autoresizingMask = animationView.autoresizingMask;
        coverView.contentMode = animationView.contentMode;
        [animationView addSubview:coverView];
        [animationView bringSubviewToFront:coverView];
        
        [UIImageView animateWithDuration:0.7 animations:^{
            coverView.alpha = 0;
        }completion:^(BOOL finished){
            [coverView removeFromSuperview];
            [coverView release];
        }];
    }

}

@end
